package com.vegetable.modules.service.outbound.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.modules.entity.outbound.SalesOrderDetail;
import com.vegetable.modules.mapper.outbound.SalesOrderDetailMapper;
import com.vegetable.modules.service.outbound.ISalesOrderDetailService;
import com.vegetable.modules.service.outbound.ISalesOrderDetailStockService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 销售订货商品表 服务实现类
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-27
 */
@Slf4j
@Service
@AllArgsConstructor
public class SalesOrderDetailServiceImpl extends ServiceImpl<SalesOrderDetailMapper, SalesOrderDetail> implements ISalesOrderDetailService {

    private ISalesOrderDetailStockService iSalesOrderDetailStockService;

    @Override
    public List<SalesOrderDetail> selectByOrderId(Long orderId) {
        QueryWrapper<SalesOrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("sales_id", orderId);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public List<SalesOrderDetail> selectByOrderIdInCloudStock(Long orderId) {
        return this.baseMapper.selectByOrderIdInCloudStock(orderId);
    }

    @Override
    public List<Long> selectDetailIdByOrderId(Long orderId) {
        List<SalesOrderDetail> details = selectByOrderId(orderId);
        List<Long> detailIds = details.stream().map(stock -> stock.getId()).collect(Collectors.toList());
        return detailIds;
    }

    @Override
    public void delDetail(Long detailId) {
        // 先删除关联的走货库存信息
        try {
            iSalesOrderDetailStockService.deleteByDetailId(detailId);
        } catch (Exception e) {
            log.error("删除销售订单详情时，删除关联走货库存信息失败: detailId={}", detailId, e);
            throw new RuntimeException("删除订单详情失败: " + e.getMessage());
        }
        // 再删除订单详情
        this.baseMapper.deleteById(detailId);
    }

    @Override
    public List<SalesOrderDetail> allShift(Long orderId) {
        QueryWrapper<SalesOrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("sales_id", orderId);
        wrapper.apply("number != shift_number");
        List<SalesOrderDetail> orderDetails =  this.baseMapper.allShift(orderId);
        return orderDetails;
    }

    @Override
    public SalesOrderDetail selectByOrderIdAndCommodityIdAndSpecsId(Long orderId, Long commodityId, Long specsId) {
        SalesOrderDetail result = new SalesOrderDetail();
        QueryWrapper<SalesOrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("sales_id", orderId);
        wrapper.eq("commodity_id", commodityId);
        if(ObjectUtil.isNull(specsId)){
            wrapper.isNull("specs_id");
        }else {
            wrapper.eq("specs_id", specsId);
        }
        List<SalesOrderDetail> list = this.baseMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            result = list.get(0);
            if(list.size() > 1){
                log.error("selectByOrderIdAndCommodityIdAndSpecsId查询异常：{}{}{}，返回了{}个结果", orderId, commodityId, specsId, list.size());
            }
        }
        return result;
    }
}
